<template>
  <el-card class="box-card" shadow="never">
    <div slot="header" class="clearfix">
      <div class="head-container">
        <div class="table_title">字典列表</div>
        <el-input v-model="query.dicName" placeholder="字典名称" size="small" class="filter-item" style="width: 200px;" @keyup.enter.native="toQuery" />
        <rrOperation />
        <crudOperation :permission="permission" />
      </div>
    </div>
    <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="570px">
      <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item label="字典名称" prop="dicName">
          <el-input v-model="form.dicName" />
        </el-form-item>
        <el-form-item label="字典编码" prop="dicCode">
          <el-input v-model="form.dicCode" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <el-table ref="table" v-loading="crud.loading" :data="crud.data[crud.listName]" highlight-current-row @current-change="handleCurrentChange">
      <el-table-column label="序号" width="60px" prop="indexNumber">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="dicName" label="字典名称" />
      <el-table-column v-permission="['admin', 'dic/edit', 'dic/delete']" label="操作" width="130px" align="center" fixed="right">
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" msg="确定删除吗,如果存在字典项则一并删除，此操作不能撤销！" />
        </template>
      </el-table-column>
    </el-table>
    <pagination />
  </el-card>
</template>

<script>
import crudDict from '@/api/system/dict'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { aid: null, dicName: null, dicCode: null, dicId: null }
export default {
  name: 'DictLeft',
  components: { crudOperation, rrOperation, udOperation, pagination },
  cruds() {
    return CRUD({ title: '字典', url: 'admin/dic/list', crudMethod: { ...crudDict }, optShow: { reset: false, add: true, edit: false, del: false, download: false, opts: true }, idField: 'dicId' })
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  props: {
    activeRow: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      permission: {
        add: ['admin', 'dic/add'],
        edit: ['admin', 'dic/edit'],
        del: ['admin', 'dic/delete']
      },
      rules: {
        dicName: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        dicCode: [
          { required: true, message: '请输入字典编码', trigger: 'blur' }
        ]
      },
      currentRow: null
    }
  },
  watch: {
    activeRow: function(val, oldVal) {
      this.currentRow = val
      if (this.currentRow !== null) {
        this.$refs.table.setCurrentRow(val)
      }
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.$emit('change', val)
    },
    [CRUD.HOOK.afterRefresh](crud) {
      if (crud.data && crud.data.content && crud.data.content.length) {
        if (this.currentRow) {
          const existRow = crud.data.content.find(row => row.dicId === this.currentRow.dicId)
          if (!existRow) {
            this.$emit('change', crud.data.content[0])
          }
        } else {
          this.$emit('change', crud.data.content[0])
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/.crud-opts {
  display: inline-flex;
}

.table_title {
  display: inline-block;
  line-height: 30px;
  margin: 0 10px 0 0;
  vertical-align: middle;
  font-weight: bold;
  color: #303133;
  font-size: 15px;
}
.head-container{
  padding-bottom: 0;
  /deep/.filter-item{
    margin-bottom: 0;
  }
}
</style>
